#include <Wire.h>
#include "HX711.h"
#include "Protocentral_MAX30205.h"

const int voiceSensorPin = A0;
const int personSensorPin = A1;
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
const int smokeSensor_AO_OutputPin = A2;
const int smokeSensor_DO_OutputPin = 4;
const int Buzzer = 5;

int incomedate = 0;
int voiceData = 0;
int personSensorValue = 0;
MAX30205 temperatureSensor;
float tempSensorValue = 0.0f;
HX711 scale;
int Astate=0;
int Dstate=0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  temperatureSensor.begin();
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  pinMode(smokeSensor_DO_OutputPin, INPUT);
  scale.set_scale(2280.f);
  scale.tare(); 
}

void loop() {
  while (Serial.available() > 0)
  {
    incomedate = Serial.read();
    if (incomedate == '1')
    {
      
      voiceData = analogRead(voiceSensorPin);
      voiceData = (long) 100 * voiceData / 1024;
      Serial.print("{");
      Serial.print('"');
      Serial.print("volume");
      Serial.print('"');
      Serial.print(":");
      Serial.print('"');
      Serial.print(voiceData);
      Serial.print('"');
      Serial.print(",");
      delay(0);

      personSensorValue = analogRead(personSensorPin);
      Serial.print('"');
      Serial.print("infrared");
      Serial.print('"');
      Serial.print(':');
      Serial.print('"');
      Serial.print(personSensorValue);
      Serial.print('"');
      Serial.print(",");
      delay(0);

      tempSensorValue = temperatureSensor.getTemperature();
      Serial.print('"');
      Serial.print("temperature");
      Serial.print('"');
      Serial.print(":");
      Serial.print('"');
      Serial.print(tempSensorValue ,2);
      Serial.print('"');
      Serial.print(",");
      delay(0);

      Serial.print('"');
      Serial.print("weight");
      Serial.print('"');
      Serial.print(":");
      Serial.print('"');
      Serial.print(scale.get_units(), 1);
      Serial.print('"');
      Serial.print(",");
      scale.power_down();
      delay(0);
      scale.power_up();
      delay(0);

      Astate=analogRead(smokeSensor_AO_OutputPin);
      Serial.print('"');
      Serial.print("smoke");
      Serial.print('"');
      Serial.print(":");
      Serial.print('"');
      Serial.print(Astate);
      Serial.print('"');
      Serial.print(",");
      delay(0);
  
      Dstate=digitalRead(smokeSensor_DO_OutputPin);
      Serial.print('"');
      Serial.print("smoke_ttl");
      Serial.print('"');
      Serial.print(":");
      Serial.print('"');
      Serial.print(Dstate);
      Serial.print('"');
      Serial.println("}");
      delay(0);

      //delay(5000);
      //Serial.println("over");
      while(1);
      }
    delay(5);
    
    if (incomedate == '2')
    {
      pinMode(Buzzer, OUTPUT);
      digitalWrite(Buzzer, HIGH);
      }

    if (incomedate == '3')
    {
      pinMode(Buzzer, OUTPUT);
      digitalWrite(Buzzer, LOW);
      }
  }
}
